namespace RSCore.Files;
public sealed record class TextFile(string Name) : RSFile(Name), IKeyConvertable
{
	public string Identity { get; set; } = Name;

	public string Text { get; set; } = string.Empty;

	public override void Deserialize(JsonValue json)
	{
		Text = json.GetText(nameof(Text));
		Identity = json.GetText(nameof(Identity));
	}

	public IEnumerable<KeyValuePair<KeyData, string>> Convert()
	{
		yield return new(new(Identity, EntryType.None, string.Empty), Text);
	}

	public override void Load(BinaryReader reader)
	{
		Identity = reader.ReadString();
		Text = reader.ReadString();
	}

	public override void Save(BinaryWriter writer)
	{
		writer.Write(Identity);
		writer.Write(Text);
	}

	public override JsonValue Serialize()
	{
		return new JsonObject
		{
			{ nameof(Text), Text },
			{ nameof(Identity), Identity }
		};
	}

	public override void Write(StreamWriter writer, RSFile? annotation)
	{
		var orig = annotation as TextFile;
		writer.WriteProperty(Identity, orig?.Identity);
		writer.WriteProperty(Text, orig?.Text);
	}

	public override void Read(StreamReader reader)
	{
		var properties = reader.ReadProperties();
		Identity = properties.GetText(nameof(Identity));
		Text = properties.GetText(nameof(Text));
	}
}